package com.javasm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javasm.common.page.PageResult;
import com.javasm.domin.criteria.AftersaleEnterDepotCriteria;
import com.javasm.domin.entity.AftersaleEnterDepot;
import com.javasm.domin.vo.AftersaleEnterDepotVo;
import com.javasm.mapper.AftersaleEnterDepotMapper;
import com.javasm.service.AftersaleEnterDepotService;
import com.javasm.service.base.impl.BaseServiceimpl;
import com.javasm.transfer.AftersaleEnterDepotTransfer;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ljy
 * @since 2022-01-16
 */
@Service
@Transactional
@RequiredArgsConstructor
public class AftersaleEnterDepotServiceImpl extends BaseServiceimpl<AftersaleEnterDepot> implements AftersaleEnterDepotService {

    private final AftersaleEnterDepotMapper aftersaleEnterDepotMapper;

    private final AftersaleEnterDepotTransfer aftersaleEnterDepotTransfer;


    @Override
    public PageResult<AftersaleEnterDepotVo> selectByPageCriteria(AftersaleEnterDepotCriteria aftersaleEnterDepotCriteria) {
        PageHelper.startPage(aftersaleEnterDepotCriteria.getCurrentPage(), aftersaleEnterDepotCriteria.getPageSize());
        LambdaQueryWrapper<AftersaleEnterDepot> lambda = new QueryWrapper<AftersaleEnterDepot>().lambda();

        if (aftersaleEnterDepotCriteria.getId() != null && aftersaleEnterDepotCriteria.getId() != 0) {
            lambda.eq(AftersaleEnterDepot::getId, aftersaleEnterDepotCriteria.getId());
        }

        if (aftersaleEnterDepotCriteria.getInType() != null && aftersaleEnterDepotCriteria.getInType() != 0) {
            lambda.eq(AftersaleEnterDepot::getInType, aftersaleEnterDepotCriteria.getInType());
        }

        if (aftersaleEnterDepotCriteria.getOutId() != null && aftersaleEnterDepotCriteria.getOutId() != 0) {
            lambda.eq(AftersaleEnterDepot::getOutId, aftersaleEnterDepotCriteria.getOutId());
        }

        if (!StringUtils.isEmpty(aftersaleEnterDepotCriteria.getStartTime())) {
            lambda.between(AftersaleEnterDepot::getInTime, aftersaleEnterDepotCriteria.getStartTime(), aftersaleEnterDepotCriteria.getEndTime());
        }

        if (aftersaleEnterDepotCriteria.getInStatus() != null ) {
            lambda.eq(AftersaleEnterDepot::getInStatus, aftersaleEnterDepotCriteria.getInStatus());
        }

        List<AftersaleEnterDepot> aftersaleEnterDepots = aftersaleEnterDepotMapper.selectList(lambda);


        PageInfo<AftersaleEnterDepot> pageInfo = new PageInfo<>(aftersaleEnterDepots);
        List<AftersaleEnterDepotVo> aftersaleEnterDepotVos = aftersaleEnterDepotTransfer.toVO(aftersaleEnterDepots);

        return new PageResult<AftersaleEnterDepotVo>(pageInfo.getTotal(), aftersaleEnterDepotVos);
    }
}
